package com.console.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.eorchis.utils.utils.PropertyUtil;

public class LoginFilter implements Filter {

	private String excludedPages;
	private String[] excludedPageArray;//需要排除的页面
	public static String loginPage;// 登录页地址
	public static String indexPage;// 网站首页地址

	public void destroy() {
	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		HttpSession session = request.getSession();
		String requestStr = getRequestString(request);
		// 判断地址是否含有非法字符
		if ("bingo".equals(guolv2(requestStr)) || "bingo".equals(guolv2(request.getRequestURL().toString()))) {
			response.sendRedirect(request.getContextPath() + "/"+LoginFilter.loginPage);
			return;
		}

		// 判断是否在过滤url之外
		boolean isExcludedPage = false;
		for (String page : excludedPageArray) {
			if (request.getServletPath().endsWith(page)) {
				isExcludedPage = true;
				break;
			}
		}

		//判断是否登录 登录为true，否则为false
		Boolean loginState = (Boolean) session.getAttribute("loginState")==null?false:(Boolean) session.getAttribute("loginState");
		if (isExcludedPage) {// 在过滤url之外
			if (loginState) {
				response.sendRedirect(LoginFilter.indexPage);
			} else {
				filterChain.doFilter(servletRequest, servletResponse);
			}
		} else {// 不在过滤url之外，判断session是否存在
//			System.out.println("被拦截的完整的地址是====" + request.getRequestURL().toString());
			if (loginState) {
				filterChain.doFilter(servletRequest, servletResponse);
				return;
			} else {
				response.sendRedirect(LoginFilter.loginPage);
				return;
			}
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		excludedPages = filterConfig.getInitParameter("excludedPages");
		if (PropertyUtil.objectNotEmpty(excludedPages)) {
			excludedPageArray = excludedPages.split(",");
		}
		return;
	}

	public static String guolv(String a) {
		a = a.replaceAll("%22", "");
		a = a.replaceAll("%27", "");
		a = a.replaceAll("%3E", "");
		a = a.replaceAll("%3e", "");
		a = a.replaceAll("%3C", "");
		a = a.replaceAll("%3c", "");
		a = a.replaceAll("<", "");
		a = a.replaceAll(">", "");
		a = a.replaceAll("\"", "");
		a = a.replaceAll("'", "");
		a = a.replaceAll("\\+", "");
		a = a.replaceAll("\\(", "");
		a = a.replaceAll("\\)", "");
		a = a.replaceAll(" and ", "");
		a = a.replaceAll(" or ", "");
		a = a.replaceAll(" 1=1 ", "");
		return a;
	}

	public String getRequestString(HttpServletRequest req) {
		String requestPath = req.getServletPath().toString();
		String queryString = req.getQueryString();
		if (queryString != null)
			return requestPath + "?" + queryString;
		else
			return requestPath;
	}

	public String guolv2(String a) {
		if (PropertyUtil.objectNotEmpty(a)) {
			if (a.contains("%22") || a.contains("%3E") || a.contains("%3e") || a.contains("%3C") || a.contains("%3c") || a.contains("<") || a.contains(">") || a.contains("\"") || a.contains("'") || a.contains("+")
					|| /* a.contains("%27") || */
					a.contains(" and ") || a.contains(" or ") || a.contains("1=1") || a.contains("(") || a.contains(")")) {
				return "bingo";
			}
		}
		return a;
	}

	static {
		// 获取properties文件
		InputStream in = LoginFilter.class.getClassLoader().getResourceAsStream("pageUrl.properties");
		// 创建一个Properties对象
		Properties prop = new Properties();
		try {
			prop.load(in);
			loginPage=(String) prop.get("loginPage");
			indexPage=(String) prop.get("indexPage");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}